package ioexception2;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class HocSinh {
	private int maSo;
	private String hoTen;
	private float diemTrungBinh;
	
	//nhap thong tin hoc sinh
	public void nhapThongTin() {
		Scanner s = new Scanner(System.in);
		
		System.out.print("Ma so: ");
		maSo = s.nextInt();
		
		System.out.print("Ho ten: ");
		hoTen = s.nextLine();
		
		System.out.print("Diem trung binh: ");
		diemTrungBinh = s.nextFloat();
	}
	
	//ghi thong tin ra file binary
	public void ghiFileBinary() {
		DataInputStream dis;
		
		try {
			FileInputStream fis = new FileInputStream("hs.text.txt");
			dis = new DataInputStream(fis);
			
			maSo = dis.readInt();
			hoTen = dis.readUTF();
			diemTrungBinh = dis.readFloat();
		}
		catch (Exception e) {
			System.out.print("Couldn't write to file.");
		}
	}
	
	//ghi thong tin ta file text
	public void ghiFileText() throws IOException {
		char[] buffer = new char[hoTen.length()];
		
		hoTen.getChars(0, hoTen.length(), buffer, 0);
		
		FileWriter fw = new FileWriter("hs.binary.txt");
		fw.write(buffer);
		fw.close();
	}
	
	//doc file binary
	public void readFileBinary(String file) {
		DataInputStream dis;
		try {
			FileInputStream fis = new FileInputStream(file);
			dis = new DataInputStream(fis);
			
			maSo = dis.readInt();
			hoTen = dis.readUTF();
			diemTrungBinh = dis.readFloat();
		}
		catch (Exception e) {
			System.out.print("Couldn't read this file.");
		}
	}
	
	public int getMaSo() {
		return maSo;
	}
	public void setMaSo(int maSo) {
		this.maSo = maSo;
	}
	public String getHoTen() {
		return hoTen;
	}
	public void setHoTen(String hoTen) {
		this.hoTen = hoTen;
	}
	public float getDiemTrungBinh() {
		return diemTrungBinh;
	}
	public void setDiemTrungBinh(float diemTrungBinh) {
		this.diemTrungBinh = diemTrungBinh;
	}
}
